package com.thinkcube.edutainment;

import java.util.ArrayList;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Activity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.bugsense.trace.BugSenseHandler;
import com.thinkcube.edutainment.client.NotificationClient;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.dto.MesageDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EActivity(R.layout.reply_dialog)
public class ReplyActivity extends Activity {

	@Extra("message")
	ArrayList<String> messageData;

	@ViewById(R.id.progressBar)
	ProgressBar progress;

	@ViewById(R.id.reply_box)
	EditText reply_box;

	@AfterViews
	void iniUi() {
		BugSenseHandler.initAndStartSession(ReplyActivity.this,
				EdutainmentConstant.BUG_SENSE_APIKEY);
		gettoken();
		setTitle(R.string.reply_act);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
	}

	@RestService
	NotificationClient notificationClient;

	@Bean
	MesageDto mesageDto;

	@Click({ R.id.send, R.id.cancel })
	void onClick(View view) {

		switch (view.getId()) {

		case R.id.send:
			if (requestToken != null) {
				progress.setVisibility(View.VISIBLE);
				for (String name : messageData) {
					send(name, requestToken);
				}

			}

			break;

		case R.id.cancel:
			finish();
			break;

		default:
			break;
		}

	}

	@Bean
	OAuthDto oAuthDto;

	@RestService
	UserClient userClient;

	private OAuth requestToken;

	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(ReplyActivity.this,
				new ReachabilityTest.Callback() {

					@Override
					public void onReachabilityTestPassed() {

						try {
							setTokenUi(userClient.getRequestToken(oAuthDto
									.getMultiValuedMap()));
						} catch (Exception e) {

							showMessge(e.getLocalizedMessage());
						}
					}

					@Override
					public void onReachabilityTestFailed() {

						showMessge("not connected ");
					}
				});

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(ReplyActivity.this, msg, Toast.LENGTH_LONG).show();
	}

	void setTokenUi(OAuth requestToken) {
		this.requestToken = requestToken;
		// getLesson(courseData2, requestToken);
	}

	@Pref
	SPreferences_ pref;

	@Trace
	@Background
	void send(String recever, OAuth requestToken) {

		mesageDto.setAuthToken(requestToken.getoAuth().getToken());

		mesageDto.setReciever(recever);

		mesageDto.setUserId(pref.getId().get());

		mesageDto.setMsg(reply_box.getText().toString());

		mesageDto.setSubject("reply");

		new ReachabilityTest(ReplyActivity.this,
				new ReachabilityTest.Callback() {

					@Override
					public void onReachabilityTestPassed() {

						try {
							doUi(notificationClient.send(mesageDto
									.getMultiValuedMap()));

						} catch (Exception e) {

							showMessge(e.getLocalizedMessage());
						}
					}

					@Override
					public void onReachabilityTestFailed() {

						showMessge("not connected ");
					}
				});

	}

	@UiThread
	void doUi(String string) {

		System.out.println("string" + string);

		progress.setVisibility(View.INVISIBLE);
		

		finish();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

		BugSenseHandler.closeSession(ReplyActivity.this);
	}

}
